<template>
  <el-dialog v-el-drag-dialog :visible.sync="showQrCode" :close-on-click-modal="false" width="640px" title="意见签名" :before-close="dialogBeforeClose">
    <div class="content" style="text-align: center;">
      <img :src="codeimg" alt="">
      <p>手机扫一扫添加意见签名</p>如果不能正常打开签名画板，请选择“使用浏览器打开”
    </div>
  </el-dialog>
</template>

<script>
export default {
    name: 'IdeaSignature',
    components: {

    },
    props: {

    },
    data() {
        return {
            codeimg: '',
            showQrCode: false,
            timer: null,
            signQrCodeTimer: null,
            pres: null,
            prej: null
        }
    },
    computed: {

    },
    watch: {

    },
    created() {},
    beforeDestroy() {},
    mounted() {},
    methods: {
        getSignature() {
            return new Promise((resolve, reject) => {
                this.pres = resolve
                this.prej = reject
                this.openSignatureQr()
            })
        },
        async openSignatureQr() {
            // 手机扫码签名
            let goUrl = window.location.protocol + '//' + window.location.host + '/mindex.html#/mPdfSignOne'

            const res = await this.$http({
                method: 'GET',
                url: 'base/paramConfig/findParamValueByName',
                params: { orgCode: 'plat', paramName: 'mpp.h5app.baseurl' }
            })
            if (res.code === 'success' && res.body.value) {
                goUrl = res.body.value + '/mindex.html#/mPdfSignOne'
                goUrl = goUrl.replace('//mindex.html', '/mindex.html')
            }

            if (['127.0.0.1', 'localhost'].some(v => ~goUrl.indexOf(v))) {
                this.$alert('手机端不能访问' + window.location.protocol + '//' + window.location.host, '操作提示', { type: 'warning' })
                return
            }

            this.$http({
                method: 'GET',
                url: 'workflow/wfInstanceNodelog/getHandWrittenSignQrCode',
                params: { signatureH5Url: goUrl }
            }).then(response => {
                if (response.code === 'success') {
                    this.codeimg = 'data:image/jpg;base64,' + response.body.image
                    this.tSessionId = response.body.tSessionId
                    if (this.timer) {
                        clearInterval(this.timer)
                        this.timer = null
                    }
                    this.timer = setInterval(() => { this.searchNewSign() }, 2000)
                    this.showQrCode = true
                    if (this.signQrCodeTimer) {
                        clearTimeout(this.signQrCodeTimer)
                        this.signQrCodeTimer = null
                    }
                    this.signQrCodeTimer = setTimeout(() => {
                        this.openSignatureQr()
                    }, 10 * 60 * 1000)
                } else {
                    this.$message.error(response.message)
                }
            })
        },
        dialogBeforeClose() {
            if (this.prej) this.prej({ code: 'cancelSign' })
            this.closeSignatureQr()
        },
        closeSignatureQr() {
            this.showQrCode = false
            clearInterval(this.timer)
            this.timer = null
            clearTimeout(this.signQrCodeTimer)
            this.signQrCodeTimer = null
            this.pres = null
            this.prej = null
        },
        searchNewSign() {
            // 查询新增签名
            this.$http({
                method: 'GET',
                url: 'workflow/wfInstanceNodelog/queryHandWrittenSign',
                params: { tSessionId: this.tSessionId }
            }).then(response => {
                if (response.code === 'success' && response.body.saved) {
                    const handWrittenSign = response.body.handWrittenSign
                    if (!handWrittenSign && this.prej) {
                        return this.prej({ message: '签名失败' })
                    }
                    const handWrittenSignO = JSON.parse(handWrittenSign)
                    if (this.pres) {
                        this.pres(handWrittenSignO.imgB64)
                    }
                    this.closeSignatureQr()
                }
            })
        }
    }
}
</script>

<style scoped lang="scss">
</style>

